#include "racing.h"
#include "ui_racing.h"
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
#include<QTimer>
int t(){
    int rand_t;
    srand((unsigned)time(NULL));
    rand_t=rand()%9;
    return rand_t;
}
Racing::Racing(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::Racing)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,180,640,800));
}

Racing::~Racing()
{
    delete ui;
}
void Racing::paintEvent(QPaintEvent *event)
{

    QPainter painter(this);
    InitCar();
    InitBarriar();
//背景
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(10,10,410,770);

    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawRect(15,15,400,760);

    painter.setPen(Qt::gray);
    for(int i=0;i<39;i++){
        painter.drawLine(15,15+20*i,415,15+20*i);
    }
    for(int i=0;i<21;i++){
        painter.drawLine(15+20*i,15,15+20*i,775);
    }
//画车
    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    painter.drawRect(CarRect);
//画障碍物
    painter.setPen(Qt::black);
    painter.setBrush(Qt::blue);
    painter.drawRect(BarriarRect);
}
void Racing::InitCar(){
    ifrun=true;
    QRect rect_1(215,755,20,20);
    CarRect = rect_1;
}
void Racing::InitBarriar(){
    ifrun=true;
    QRect rect_2(15+t()*40,15,20,20);
    BarriarRect = rect_2;
    timer=new QTimer(this);
    timer->start(500);
    connect(timer,SIGNAL(timout()),SLOT(Barriar_update()));
}
void Racing::Barriar_update(){
    BarriarRect.setTop(BarriarRect.top()+20);
    BarriarRect.setBottom(BarriarRect.bottom()+20);
    update();
}









